<template>
  <div class="questions-from">
    <!-- from 表单 -->
    <el-form
      ref="simpleFormRef"
      :model="simpleForm"
      label-width="80px"
      size="small"
    >
      <el-row>
        <el-col :span="6">
          <el-form-item label="学科" prop="subjectID">
            <el-select
              v-model="simpleForm.subjectID"
              placeholder="请选择"
              style="width:100%"
              @change="onSelectSubject(simpleForm.subjectID)"
            >
              <el-option
                v-for="subject in subjectList"
                :key="subject.value"
                :label="subject.label"
                :value="subject.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="二级目录" prop="catalogID">
            <el-select
              v-model="simpleForm.catalogID"
              placeholder="请选择"
              style="width:100%"
            >
              <el-option
                v-for="item in DirectoryList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="标签" prop="tags">
            <el-select
              v-model="simpleForm.tags"
              placeholder="请选择"
              style="width:100%"
            >
              <el-option
                v-for="item in tagList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="关键字" prop="keyword">
            <el-input
              v-model="simpleForm.keyword"
              placeholder="根据题干搜索"
            ></el-input>
          </el-form-item>
        </el-col>
        <!-- 试题类型 -->
        <el-col :span="6">
          <el-form-item label="试题类型" prop="questionType">
            <el-select
              v-model="simpleForm.questionType"
              placeholder="请选择"
              style="width:100%"
            >
              <el-option
                v-for="item in questionType"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="难度" prop="difficulty">
            <el-select
              v-model="simpleForm.difficulty"
              placeholder="请选择"
              style="width:100%"
            >
              <el-option
                v-for="item in difficulty"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="方向" prop="direction">
            <el-select
              v-model="simpleForm.direction"
              placeholder="请选择"
              style="width:100%"
            >
              <el-option
                v-for="item in direction"
                :key="item.value"
                :label="item"
                :value="item"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="录入人" prop="creatorID">
            <el-select
              v-model="simpleForm.creatorID"
              placeholder="请选择"
              style="width:100%"
            >
              <el-option
                v-for="item in creatorList"
                :key="item.id"
                :label="item.username"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <!-- 题目备注 -->
        <el-col :span="6">
          <el-form-item label="题目备注" prop="remarks">
            <el-input v-model="simpleForm.remarks"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="企业简称" prop="shortName">
            <el-input v-model="simpleForm.shortName"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="城市" prop="province">
            <el-select
              class="city-select"
              v-model="simpleForm.province"
              @change="handleProvince"
            >
              <el-option
                v-for="item in citySelect.province"
                :key="item"
                :label="item"
                :value="item"
              ></el-option>
            </el-select>
            <el-select
              style="width: 50%"
              v-model="simpleForm.city"
              placeholder="请选择"
            >
              <el-option
                v-for="item in citySelect.cityDate"
                :key="item"
                :label="item"
                :value="item"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item class="btns-right">
            <el-button size="small" @click="onClear">清除</el-button>
            <el-button
              type="primary"
              size="small"
              @click="$emit('search', simpleForm)"
              >搜索</el-button
            >
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <!-- / from 表单 -->
  </div>
</template>

<script>
import { simple } from '@/api/hmmm/subjects'
import { simple as simpleDirectorys } from '@/api/hmmm/directorys'
import { simple as simpleTags } from '@/api/hmmm/tags'
import { simple as simpleUsers } from '@/api/base/users'
import { questionType, difficulty, direction } from '@/api/hmmm/constants'
import { provinces, citys } from '@/api/hmmm/citys.js'

export default {
  name: 'QuestionsFrom',

  data() {
    return {
      simpleForm: {
        subjectID: null, // 学科
        catalogID: null, // 二级目录
        tags: null, // 标签
        keyword: null, // 关键字
        questionType: null, // 试题类型
        difficulty: null, // 难度
        direction: null, // 方向
        creatorID: null, // 录入人
        remarks: null, // 题目备注
        shortName: null, // 企业简称
        province: null, // 市
        city: null // 区
        /*  page: 1,
        pagesize: 5 */
      },
      subjectList: [], // 学科列表
      DirectoryList: [], // 二级目录列表
      tagList: [], // 标签列表
      creatorList: [], // 录入人列表
      questionType: questionType,
      difficulty: difficulty,
      direction: direction,
      citySelect: {
        province: [],
        cityDate: []
      } /* ,
      totalCount: 0, // 试题总条数
      questionsData: [] */
    }
  },
  created() {
    this.getSubjects()
    this.getCreator()
    this.getCityData()
  },
  methods: {
    async getSubjects() {
      const { data: res } = await simple()
      this.subjectList = res
    },
    async onSelectSubject(subjectID) {
      const { data: res1 } = await simpleDirectorys({
        subjectID
      })
      this.DirectoryList = res1
      const { data: res2 } = await simpleTags({
        subjectID
      })
      this.tagList = res2
    },
    async getCreator() {
      const { data: res } = await simpleUsers()
      console.log(res)
      this.creatorList = res
    },
    // 获取省
    getCityData() {
      this.citySelect.province = provinces()
    },
    // 选省获取到市
    handleProvince(e) {
      this.simpleForm.city = ''
      this.citySelect.cityDate = citys(e)
    },

    // 重置表单
    onClear() {
      console.log(this.$refs.simpleFormRef)
      this.$refs.simpleFormRef.resetFields()
      this.simpleForm.city = ''
      this.DirectoryList = []
      this.tagList = []
      // this.getAllQuestions()
      this.$emit('search', this.simpleForm)
    }
  }
}
</script>

<style scoped>
.city-select {
  width: 48%;
  margin-right: 2%;
}
.btns-right {
  text-align: right;
}
</style>
